package com.example.security.config;

import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.example.security.dmo.SysUser;
import com.example.security.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * token验证处理
 *
 * @author taixin
 */
@Component
public class TokenService {

    @Autowired
    private RedisService redisService;

    public String createToken(SysUser user){
        String token = UUID.fastUUID().toString();
        // 保存或更新用户token
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("access_token", token);
        map.put("expires_in", 7200);
        redisService.setCacheObject(token, JSONUtil.toJsonStr(user), 7200L, TimeUnit.SECONDS);
        return token;
    }
}